#include "md_gateway.hpp"

#include "Logger.hpp"

int count = 0;

std::atomic<bool> flag(false);

void MdGateway::handleMarkPrice(std::any data)
{

	auto priceData = std::any_cast<MarkPriceStream>(data);

	for (auto &price : priceData.prices)
	{
		if (price["s"] == m_symbol)
		{
			m_markPriceString = price["p"];
			logg("m_markPriceString:" + m_markPriceString);
			if (count++ > 5) {
				logg("----------------try to notify_all");
				flag = true;
				m_priceSet.notify_all();
			}
			break;
		}
	}
}

void MdGateway::run()
{

	auto funcMarkPrice = std::bind(&MdGateway::handleMarkPrice, std::ref(*this), std::placeholders::_1);
	m_market.monitorMarkPrice(funcMarkPrice);

	logg("----------------Waiting");
	// std::this_thread::sleep_for(5s);

	std::unique_lock<std::mutex> lk(m_mutex);
	m_priceSet.wait(lk, [] { return flag.load(); });

	logg("----------------Waiting end");

}
